package com.apes.hr.base.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.hr.base.model.Leave;
import com.apes.hr.base.repository.LeaveRepository;
import com.apes.scm.rbac.model.User;
import com.apes.scm.rbac.service.UserService;
import org.flowable.engine.ProcessEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.util.Date;

/**
 * 功能：hr-请假单
 *
 * @author zyh
 * @create 2018-7-11 11:39:24
 */
@Service("hrLeaveService")
public class LeaveService extends DomainService {
    @Autowired
    public LeaveRepository leaveRepository;
    @Autowired
    EntityManager entityManager;
    @Autowired
    UserService userService;
    @Autowired
    private ProcessEngine engine;


    /***crm.customerCar.findAll
     * 功能：创建
     * @param request
     * @return
     */
    public Leave create(SimpleRequest request) {
        try{
            Leave leave = request.getO(Leave.class);
            leave.setState("create");  //设置当前的状态
            return leaveRepository.saveAndFlush(leave);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    /***
     * 功能： 修改
     * @param request
     * @return
     */
    public Leave update(SimpleRequest request) {
        String id = request.get("id");
        long versions = (int) request.get("version"); // 获取当前请求的版本信息
        Leave Leave = leaveRepository.findOne(id);
        if (Leave == null) throw new RuntimeException("请假申请单【" + id + "】在系统中不存在。");
        if(Leave.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");  // 用于判断当前版本是否被修改
        Leave requestLeave = request.getO(Leave.class);
        requestLeave = leaveRepository.saveAndFlush(requestLeave);
        return requestLeave;
    }

    /***
     * 功能： 删除
     * @param request
     * @return
     * @throws RuntimeException
     */
    public String delete(SimpleRequest request) throws RuntimeException {
        String id = request.get("id");
        long versions = (int) request.get("version"); // 获取当前请求的版本信息
        Leave Leave = leaveRepository.findOne(id);
        if (Leave == null) throw new RuntimeException("请假申请单【" + id + "】在系统中不存在。");
        if(Leave.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");  // 用于判断当前版本是否被修改

        // 设置删除人以及状态
        Leave requestLeave = request.getO(Leave.class);

        requestLeave.setDeleteId(this.getPerson(request)); //设置人员
        requestLeave.setDeleteDate(new Date()); // 设置时间
        requestLeave.setState("delete");  //设置当前的状态为删除
        leaveRepository.saveAndFlush(requestLeave);

        return "删除成功！";
    }


    /***
     * 功能：回车查询
     * @param request
     * @return
     */
    public Leave findOne(SimpleRequest request) {
        Leave leave = leaveRepository.findOne((String) request.get("id"));
        return leave;
    }

    /***
     * 功能 转OA start 节点
     * @param request
     * @return
     * @throws RuntimeException
     */
    public Leave transOA(SimpleRequest request) throws RuntimeException{
        //设置单据状态为处理中
        Leave leave = request.getO(Leave.class);  //设置javaBean 并格式化时间
        // 设置转OA人
        leave.setInitiatOaId(this.getPerson(request)); //设置人员
        leave.setInitiatOaDate(new Date()); // 设置时间
        leave.setState("disposing");  //设置当前的状态为处理中
        leave.setNextDisposeSign( String.valueOf(((User)this.getCurrentUser(request)).getId()) );  //设置客诉节点的审批人
        leaveRepository.saveAndFlush(leave);

        return leave;
    }


    /***
     * 功能 部门处理节
     * @param request
     * @return
     * @throws RuntimeException
     */

    public Leave transDispose(SimpleRequest request) {

        JSONObject tempJson = request.getJO();
        if( tempJson.get("nextDisposeSign") == null) throw  new RuntimeException("节点候选人不能为空");

        //设置单据状态为处理中
        //获取最新的数据并设置状态
        Leave Leave = leaveRepository.findOne((String)request.get("id"));
        Leave.setState("disposing");  //设置当前的状态为处理中
        return leaveRepository.saveAndFlush(Leave);

    }

    public Leave afterDispose(SimpleRequest request) {
        Leave Leave = leaveRepository.findOne((String)request.get("id"));
        Leave.setNextDisposeSign(String.valueOf(((User)this.getCurrentUser(request)).getId()));
        return leaveRepository.saveAndFlush(Leave); //设置客诉节点的审批人
    }






    /***
     * 功能 end节点
     * @param request
     * @return
     * @throws RuntimeException
     */
    public Leave transEnd(SimpleRequest request) throws RuntimeException{

        //设置单据状态
        Leave leave = request.getO(Leave.class);  //设置javaBean 并格式化时间
        leave.setState("end");  //设置当前的状态为结单

        //设置结单人，结单日期
        leave.setSprId(this.getPerson(request));
        leave.setSprDate( new Date());

        return leaveRepository.saveAndFlush(leave);
    }

}
